/*
 * Copyright (c) 2007-2008, Thomas Krause (tho.krause@gmail.com)
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *     * Neither the name of gidoo nor the names of its contributors may be used to
 *       endorse or promote products derived from this software without specific
 *       prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.gidoo.cms.helper;

import org.apache.wicket.authorization.strategies.role.Roles;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.gidoo.cms.admin.AdminPage;
import org.gidoo.cms.editor.EditorPage;
import org.gidoo.cms.login.GidooCMSSession;
import org.gidoo.cms.publisher.PublisherPage;

/**
 * A helper panel that shows links to the administration functionalities the user
 * may access.
 * 
 * @author <a href="mailto:tho.krause@gmail.com>Thomas Krause</a>
 */
public class AdminNavPanel extends Panel
{

  /**
   * Constructor.
   * @param id The wicket ID.
   * @param parentPageClass Wether the parent is a admin-, edit- or publish-page.
   */
  public AdminNavPanel(String id, Class parentPageClass)
  {
    super(id);

    Link lnkAdmin = new Link("lnkAdmin")
    {

      @Override
      public void onClick()
      {
        setResponsePage(AdminPage.class);
      }
    };
    
    add(lnkAdmin);
    
    Link lnkEdit = new Link("lnkEdit")
    {

      @Override
      public void onClick()
      {
        setResponsePage(EditorPage.class);
      }
    };
    
    add(lnkEdit);
    
    Link lnkPublish = new Link("lnkPublish")
    {

      @Override
      public void onClick()
      {
        setResponsePage(PublisherPage.class);
      }
    };
    
    add(lnkPublish);
    
    Label lblSeparator1 = new Label("lblSeparator1", new Model(" | "));
    add(lblSeparator1);
    
    Label lblSeparator2 = new Label("lblSeparator2", new Model(" | "));
    add(lblSeparator2);
    
    // set visibility and if enabled //
    
    // link on own page?
    if(parentPageClass == AdminPage.class)
    {
      lnkAdmin.setEnabled(false);
    }
    else if(parentPageClass == EditorPage.class)
    {
      lnkEdit.setEnabled(false);
    }
    else if(parentPageClass == PublisherPage.class)
    {
      lnkPublish.setEnabled(false);
    }
    
    // access allowed?
    GidooCMSSession session = (GidooCMSSession) getSession();
    Roles r = session.getRoles();
    
    if(r.hasRole(Roles.ADMIN))
    {
      lnkAdmin.setVisible(true);
    }
    else
    {
      lnkAdmin.setVisible(false);
    }
    
    if(r.hasRole(GidooCMSSession.ROLE_EDITOR))
    {
      lnkEdit.setVisible(true);
    }
    else
    {
      lnkEdit.setVisible(false);
    }
    
    if(r.hasRole(GidooCMSSession.ROLE_PUBLISHER))
    {
      lnkPublish.setVisible(true);
    }
    else
    {
      lnkPublish.setVisible(false);
    }
    
    // set separator visibility
    lblSeparator1.setVisible(false);
    lblSeparator2.setVisible(false);
    
    if(
      r.hasRole(Roles.ADMIN) && r.hasRole(GidooCMSSession.ROLE_EDITOR) 
      && r.hasRole(GidooCMSSession.ROLE_PUBLISHER)
      )
    {
      lblSeparator1.setVisible(true);
      lblSeparator2.setVisible(true);
    }
    else if(r.hasRole(Roles.ADMIN) && r.hasRole(GidooCMSSession.ROLE_EDITOR))
    {
      lblSeparator1.setVisible(true);
    }
    else if(r.hasRole(GidooCMSSession.ROLE_EDITOR) && r.hasRole(GidooCMSSession.ROLE_PUBLISHER))
    {
      lblSeparator2.setVisible(true);
    }
    else if(r.hasRole(Roles.ADMIN) && r.hasRole(GidooCMSSession.ROLE_PUBLISHER))
    {
      lblSeparator1.setVisible(true);
    }
  }
}
